﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tensorflow.Keras;
using Tensorflow.Keras.Engine;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;

namespace TensorFlowTest.BasicModels;

public class LogisticRegressionKeras : SciSharpExample, IExample
{
    ICallback result;

    ExampleConfig IExample.InitConfig()
    {
        Config = new ExampleConfig()
        {
            Name = "Logistic Regression (Keras)",
            Enabled = true,
            IsImportingGraph = false,
        };
        return Config;
    }

    bool IExample.Run()
    {
        tf.enable_eager_execution();

        //prepare MNIST data.
        var ((x_train, y_train), (x_test, y_test)) =
            keras.datasets.mnist.load_data();

        (x_train, x_test) = (x_train / 255f, x_test / 255f);

        var model = keras.Sequential(new List<ILayer>
        {
            keras.layers.Flatten(),
            keras.layers.Dense(10,activation:"softmax")
        });

        model.compile(optimizer: keras.optimizers.SGD(0.01f),
            loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
            metrics: new[] { "accuracy" });

        result = model.fit(x_train, y_train, epochs: 5);

        var predicted = model.predict(x_test);

        return true;

    }
}
